package com.ncloud.param.service.impl;

import java.util.List;

import com.ncloud.param.service.CommparaService;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.RedisUtils;
import com.ncloud.param.entity.Commpara;
import com.ncloud.param.mapper.CommparaMapper;

@Service
public class CommparaServiceImpl implements CommparaService {
	
	@Autowired
	private CommparaMapper mapper;
	@Autowired
	private RedisUtils redisUtils;
	
	public JSONArray getBsCommparaSvcInfo(JSONObject json){
		//判断必传参数是否存在
		if(!json.containsKey("subsysCode") || !json.containsKey("paramAttr") || !json.containsKey("paramCode")){
			throw new UserException("2001", "传入参数有误");
		}
		
		String subsysCode = json.getString("subsysCode");
		String paramAttr = json.getString("paramAttr");
		String paramCode = json.getString("paramCode");
	    String para_code1 = json.containsKey("paraCode1") ? json.getString("paraCode1") : null;
	    String para_code2 = json.containsKey("paraCode2") ? json.getString("paraCode2") : null;
	    String para_code3 = json.containsKey("paraCode3") ? json.getString("paraCode3") : null;
	    String para_code4 = json.containsKey("paraCode4") ? json.getString("paraCode4") : null;
	    
	    JSONArray array = null;
	    String key = "COMMPARA_"+subsysCode+"_"+paramAttr+"_"+paramCode+"_"+para_code1+"_"+para_code2+"_"+para_code3+"_"+para_code4;
	    if(redisUtils.exists(key)){
	    	array = (JSONArray) redisUtils.get(key);
	    }else{
	    	List<Commpara> params = mapper.getInfoByParam(subsysCode,paramAttr,paramCode,para_code1,para_code2,para_code3,para_code4);
	    	array = JSON.parseArray(JSON.toJSONString(params));
	    	redisUtils.set(key, array);
	    }
	    
		return array;
	}
}
